﻿using RotovapPlatform.Domain.Common;
using RotovapPlatform.Domain.Equipment.Device.Motors;
using RotovapPlatform.Domain.Equipment.Device.Motors.Dto;
using RotovapPlatform.Domain.Equipment.Interfaces.Base;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace RotovapPlatform.Domain.Equipment.Interfaces.Services
{
	/// <summary>
	/// 电机控制服务接口，提供基于工作位置的电机控制功能
	/// </summary>
	public interface IMotorControlService
	{
		/// <summary>
		/// 获取所有电机列表
		/// </summary>
		/// <returns>电机列表</returns>
		Task<IEnumerable<IMotorBase>> GetMotorsAsync();

		/// <summary>
		/// 获取所有已注册的电机
		/// </summary>
		/// <returns>电机列表</returns>
		Task<List<IMotorBase>> GetRegisteredMotorsAsync();

		/// <summary>
		/// 获取特定ID的电机
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>电机实例</returns>
		Task<IMotorBase> GetMotorAsync(string motorId);

		/// <summary>
		/// 初始化电机控制服务
		/// </summary>
		/// <returns>操作结果</returns>
		Task<OperationResult> InitializeAsync();

		/// <summary>
		/// 使能电机
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> EnableMotorAsync(string motorId);

		/// <summary>
		/// 去使能电机
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> DisableMotorAsync(string motorId);

		/// <summary>
		/// 电机回零
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> HomeMotorAsync(string motorId);

		/// <summary>
		/// 移动电机到指定工作位置
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="positionName">工作位置名称</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> MoveToPositionAsync(string motorId, string positionName);

		/// <summary>
		/// 移动电机到指定位置
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="position">目标位置</param>
		/// <param name="speed">移动速度</param>
		/// <param name="acceleration">加速度</param>
		/// <param name="deceleration">减速度</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> MoveToPositionAsync(string motorId, double position, double speed, double acceleration, double deceleration);

		/// <summary>
		/// 判断电机是否在指定工作位置
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="positionName">工作位置名称</param>
		/// <returns>操作结果，包含是否在指定位置的信息</returns>
		Task<OperationResult<bool>> IsAtPositionAsync(string motorId, string positionName);

		/// <summary>
		/// 电机JOG+运动
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="speed">运行速度（可选），如果不指定则使用电机参数中定义的jogSpeed</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> JogPlusAsync(string motorId, double? speed = null);

		/// <summary>
		/// 电机JOG-运动
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="speed">运行速度（可选），如果不指定则使用电机参数中定义的jogSpeed</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> JogMinusAsync(string motorId, double? speed = null);

		/// <summary>
		/// 停止电机
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> StopMotorAsync(string motorId);

		/// <summary>
		/// 复位轴
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> ResetAxisAsync(string motorId);

		/// <summary>
		/// 获取电机状态
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>电机状态</returns>
		Task<MotorStatus> GetMotorStatusAsync(string motorId);

		/// <summary>
		/// 获取所有已配置的电机ID
		/// </summary>
		/// <returns>电机ID集合</returns>
		Task<IEnumerable<string>> GetAllMotorIdsAsync();

		/// <summary>
		/// 获取电机的所有工作位置
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>工作位置名称集合</returns>
		Task<IEnumerable<string>> GetMotorPositionsAsync(string motorId);

		/// <summary>
		/// 学习当前位置
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="positionName">位置名称</param>
		/// <param name="speed">移动速度</param>
		/// <param name="acceleration">加速度</param>
		/// <param name="deceleration">减速度</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> LearnCurrentPositionAsync(string motorId, string positionName, double? speed = null, double? acceleration = null, double? deceleration = null);

		/// <summary>
		/// 保存电机位置配置
		/// </summary>
		/// <returns>操作结果</returns>
		Task<OperationResult> SavePositionConfigAsync();

		/// <summary>
		/// 加载电机位置配置
		/// </summary>
		/// <returns>操作结果</returns>
		Task<OperationResult> LoadPositionConfigAsync();

		/// <summary>
		/// 获取电机位置配置详情
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="positionName">位置名称</param>
		/// <returns>位置配置</returns>
		Task<MotorPositionConfig> GetPositionConfigAsync(string motorId, string positionName);

		/// <summary>
		/// 更新位置配置
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="positionName">位置名称</param>
		/// <param name="config">位置配置</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> UpdatePositionConfigAsync(string motorId, string positionName, MotorPositionConfig config);

		/// <summary>
		/// 删除位置配置
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="positionName">位置名称</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> DeletePositionAsync(string motorId, string positionName);

		/// <summary>
		/// 执行电机扭矩控制
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <param name="targetTorque">目标扭矩（千分比）</param>
		/// <param name="torqueChangeRate">扭矩变化率</param>
		/// <param name="maxSpeed">扭矩模式下的最大速度（脉冲/秒）</param>
		/// <param name="timeoutSeconds">超时时间秒</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> ExecuteTorqueControlAsync(string motorId, short targetTorque, double torqueChangeRate, uint maxSpeed, int timeoutSeconds = 20);

		/// <summary>
		/// 执行打液操作
		/// </summary>
		/// <param name="motorId">泵电机ID</param>
		/// <param name="volumeML">打液量(ml)</param>
		/// <param name="conversionFactor">转换因子(ml/圈)，表示电机每转一圈能够输出的液体量</param>
		/// <param name="speedRPM">打液速度(RPM-每分钟转数)</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> DispensingLiquidAsync(string motorId, double volumeML, double conversionFactor, double speedRPM = 60.0);

		/// <summary>
		/// 暂停打液操作
		/// </summary>
		void PauseDispensing();

		/// <summary>
		/// 恢复打液操作
		/// </summary>
		void ResumeDispensing();

		/// <summary>
		/// 获取当前已打液量
		/// </summary>
		/// <returns>当前已打液量(ml)</returns>
		double GetCurrentDispensedVolume();

		/// <summary>
		/// 重置打液量计数
		/// </summary>
		void ResetDispensedVolume();

		/// <summary>
		/// 获取打液是否处于暂停状态
		/// </summary>
		/// <returns>是否暂停</returns>
		bool IsDispensingPaused();


	}
}